const { getOrderList, getReportTypeList } = require('../../utils/api.js');
const { formatDate } = require('../../utils/tool');

Page({
  data: {
    typeMap: {},
    orderList: [],
  },

  onLoad() {
    getReportTypeList().then((res) => {
      const typeList = (res?.data || []).map((item) => {
        return {
          dictLabel: item.dictLabel,
          dictType: item.dictType,
        };
      });
      const typeMap = typeList.reduce((acc, cur) => {
        acc[cur.dictType] = cur.dictLabel;
        return acc;
      }, {});
      this.setData({ typeMap });
    });

    getOrderList().then((res) => {
      this.setData({
        orderList: (res.data || []).map((item) => {
          return {
            ...item,
            imgs: item.imgs?.split?.(',') || [],
            createTime: formatDate(item.createTime),
          };
        }),
      });
    });
  },

  handleStatusChange() {
    wx.showModal({
      title: '提示',
      content: '确定要接此订单吗？',
      success: (res) => {
        if (res.confirm) {
          const { item, status } = e.currentTarget.dataset;
          const params = {
            id: item.id,
            status: status,
          };
          updateOrderStatus(params).then(() => {
            wx.showToast({ title: '操作成功', icon: 'success' });
          });
        }
      },
    });
  },
});
